<template>
  <div class="login">
    <div class="loginBox">
      <p>后台管理系统</p>
      <el-form
        :model="ruleForm"
        status-icon
        :rules="rules"
        ref="ruleForm"
        label-width="60px"
        class="demo-ruleForm"
      >
        <el-form-item label="账号" prop="adminname">
          <el-input
            type="text"
            v-model="ruleForm.adminname"
            autocomplete="off"
          ></el-input>
        </el-form-item>
        <el-form-item label="密码" prop="password">
          <el-input
            type="password"
            v-model="ruleForm.password"
            autocomplete="off"
          ></el-input>
        </el-form-item>
        <el-form-item>
          <el-button type="primary" @click="submitForm('ruleForm')"
            >提交</el-button
          >
          <el-button @click="resetForm('ruleForm')">重置</el-button>
        </el-form-item>
      </el-form>
    </div>
  </div>
</template>

<script>
// 导入登录接口
import { loginApi } from "@/api/http.js";
// 导入辅助函数
import { mapMutations } from "vuex";

export default {
  name: "",
  data() {
    return {
      ruleForm: {
        adminname: "",
        password: "",
      },
      rules: {
        adminname: [
          { required: true, message: "请输入用户名", trigger: "blur" },
        ],
        password: [{ required: true, message: "请输入密码", trigger: "blur" }],
      },
    };
  },
  methods: {
    ...mapMutations(["updateUsername", "updataRole", "updateCheckedkeys"]),
    //  登录
    submitForm(formName) {
      this.$refs[formName].validate((valid) => {
        if (valid) {
          // 调用登录接口
          loginApi(this.ruleForm).then((res) => {
            // 将返回的数据解构出来,账号，权限，角色，token
            let { adminname, checkedkeys, role, token } = res.data.data;
            // 如果登录成功
            if (res.data.code == 200) {
              // 将用户名传入vuex
              this.updateUsername(adminname);
              // 将权限传入vuex
              this.updateCheckedkeys(checkedkeys);
              // 将角色传入vuex
              this.updataRole(role);
              // 将token值存到本地
              localStorage.setItem("token", token);
              // 存储完毕后，跳转页面
              this.$router.push("/home");
            }
          });
        } else {
          return false;
        }
      });
    },
    //  重置
    resetForm(formName) {
      this.$refs[formName].resetFields();
    },
  },
};
</script>
<style scoped lang='scss'>
* {
  margin: 0;
  padding: 0;
}
.login {
  width: 100%;
  height: 100%;
  background: url("@/assets/img/1.jpg") no-repeat center center/100% 100%;
  display: flex;
  justify-content: center;
  align-items: center;

  .loginBox {
    width: 500px;
    height: 400px;
    background: white;
    border-radius: 10px;

    > p {
      width: 100%;
      height: 80px;
      background: yellowgreen;
      text-align: center;
      line-height: 80px;
      font-size: 30px;
      color: white;
      border-radius: 10px 10px 0 0;
    }
  }
  .el-form {
    padding: 50px;
    box-sizing: border-box;
  }
  .el-form-item {
    margin-bottom: 24px;
    height: 50px;
  }
  .el-button {
    width: 80px;
  }
  .el-form-item__label {
    line-height: 50px;
  }
}
</style>